home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 201-225 / disk_222 / plplot / src / source.zoo / plhist.c < prev    next >
C/C++ Source or Header  |  1989-05-15  |  1KB  |  46 lines

  1. /* Draws a histogram of n values of a variable in array data(1..n) */
  2. /* in the range datmin to datmax using nbin bins. If "oldwin" is   */
  3. /* true, the histogram is plotted in the current window. If not,   */
  4. /* the routine calls "plenv" to set up the graphics environment.   */
  5.  
  6. #include "plplot.h"
  7. #include <math.h>
  8.  
  9. void plhist(n,data,datmin,datmax,nbin,oldwin)
  10. int n, nbin;
  11. int oldwin;
  12. float data[], datmin, datmax;
  13. {
  14.       int i, bin, level;
  15.       float x[201], y[201], dx, ymax;
  16.       
  17.       glev(&level);
  18.       if (level<1) fatal("Please call PLSTAR before PLHIST.");
  19.       if (level<3 && oldwin)
  20.               fatal("Please set up window before calling PLHIST.");
  21.       
  22.       if (nbin < 1 || nbin > 200)
  23.               fatal("Cannot have <1 or >200 bins in PLHIST.");
  24.       if (datmin >= datmax)
  25.               fatal("Data range invalid in PLHIST.");
  26.       
  27.       dx = (datmax-datmin) / nbin;
  28.       for (i=0; i<=nbin; i++) {
  29.         x[i] = datmin + i*dx;
  30.         y[i] = 0.0;
  31.       }
  32.  
  33.       for (i=0; i<n; i++)  {
  34.         bin = (data[i] - datmin)/dx;
  35.         if (bin >= 0 && bin < nbin) y[bin]++;
  36.       }
  37.  
  38.       if (!oldwin) {
  39.         ymax = 0.0;
  40.         for (i=0; i<nbin; i++) ymax = max(ymax,y[i]);
  41.         plenv(datmin,datmax,0.0,1.1*ymax,0,0);
  42.       }
  43.          
  44.       plbin(nbin+1,x,y,0);
  45. }
  46.